<?php

/**
 * Created by PhpStorm.
 * User: YiivonHome
 * Date: 2016-05-27
 * Time: 15:41
 */
if (!defined('ACCESS')) {
    exit('Access denied.');
}

define('VISITED_COOKIE_TOKEN', 'VISITED_COOKIE');

class Wxgame extends WxgameBase
{

    public static function test()
    {
        return "test on wxgame";
    }

    private static function isAjax()
    {
        return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
    }

    public static function getGames()
    {
        // 以下两种方式均可以访问 sample 的 DB
        $db = self::__instance();
        // $db = self::__instance(SAMPLE_DB_ID);

        $sql = "select * from " . OSA_TABLE_PREFIX . "wxgame_game order by create_time desc";
        $list = $db->query($sql)->fetchAll();
        if ($list) {
            return $list;
        }
        return array();
    }

    public static function getPlayers()
    {
        // 以下两种方式均可以访问 sample 的 DB
        $db = self::__instance();
        // $db = self::__instance(SAMPLE_DB_ID);

        $sql = "select * from " . OSA_TABLE_PREFIX . "wxgame_player order by create_time desc";
        $list = $db->query($sql)->fetchAll();

        if ($list) {
            for ($i = 0; $i < sizeof($list); $i++) {
                $list[$i]["detial"] = json_decode($list[$i]["detial"], true);
            }
         //   var_dump($list); die();
            return $list;
        }
        return array();
    }

    public static function addVisited()
    {
        extract($_GET);
        if (!isset($gid)) {
            return false;
        }

        if (!isset($_COOKIE[VISITED_COOKIE_TOKEN])) {
            $token = uniqid('VISITED', true);
            setcookie(VISITED_COOKIE_TOKEN, $token, time() + 60 * 60 * 24 * 30);
        } else {
            $token = $_COOKIE[VISITED_COOKIE_TOKEN];
        }

        $db = self::__instance();

        $sql = "UPDATE  `osa_wxgame_game` SET `visited_count` =  `visited_count` + '1' WHERE  `name` = '$gid'";
        $db->query($sql);


        return true;
    }

    public static function addPlayer()
    {
        extract($_GET);
        if (!isset($gid)) {
            return false;
        }

        if (!isset($_COOKIE[VISITED_COOKIE_TOKEN])) {
            $token = uniqid('VISITED', true);
            setcookie(VISITED_COOKIE_TOKEN, $token, time() + 60 * 60 * 24 * 30);
        } else {
            $token = $_COOKIE[VISITED_COOKIE_TOKEN];
        }

        $db = self::__instance();

        // played count
        $sql = "UPDATE  `osa_wxgame_game` SET `played_count` =  `played_count` + '1' WHERE  `name` = '$gid'";
        $db->query($sql);

        $detail = new stdClass();
        $detail->ua = $_SERVER['HTTP_USER_AGENT'];
        $detail->ip = $_SERVER['SERVER_ADDR'];

        if (isset($score)) {
            $detail->score = $score;
        }
        if (!isset($status_text)) $status_text = 'unknow';

        $items = $db->query(sprintf("SELECT * FROM `osa_wxgame_player` WHERE `cookie` = '%s' AND `game_name` = '%s'", $token, $gid))->fetchAll();
        if (sizeof($items) > 0) {
            // update
            $sql = sprintf("UPDATE `osadmin`.`osa_wxgame_player` SET `detial` = '%s', `status_text` = '%s', `last_time` = NOW() WHERE `id` = %d;", json_encode($detail), $status_text, $items[0]["id"]);
            $db->query($sql);
        } else {
            $sql = sprintf("INSERT INTO  `osa_wxgame_player` (`id`, `cookie`, `name`, `detial`, `status`, `status_text`, `game_name`, `last_time`, `create_time`)
                        VALUES (NULL, '%s', 'unknow', '%s', '1', '%s', '%s', NOW(), NOW());", $token, json_encode($detail), $status_text, $gid);

            $db->query($sql);
        }

        return true;
    }
}